package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    /**
     * 查询分类信息
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        //获取Redis中操作String类型数据的对象
        ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
        //从Redis中取值
        List<Category> categoryList = (List<Category>) valueOperations.get("category");
        if (categoryList == null) {
            LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
            // 当type值不为空时,拼接type条件
            wrapper.eq(category.getType() != null, Category::getType, category.getType());
            // 设置排序规则
            wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
            List<Category> list = categoryService.list(wrapper);
            //向Redis中存值
            valueOperations.set("category", list);
            return R.success(list);
        }
        return R.success(categoryList);
    }


    /**
     * 修改分类信息
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改的数据为: {} ",category.toString());
        categoryService.updateById(category);
        //进行mysql数据库增删改时，Redis应该清除相应的key
        redisTemplate.delete("category");
        return R.success("修改成功");
    }

    /**
     * 根据id删除分类信息
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam("ids") Long id){
        log.info("要删除的id值为:{} ",id);
        //categoryService.removeById(id);
        categoryService.remove(id);
        //进行mysql数据库增删改时，Redis应该清除相应的key
        redisTemplate.delete("category");
        return R.success("删除分类成功");
    }


    /**
     * 分页查询分类信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize){
        log.info("请求参数: {} {}", page, pageSize);
        //构造Category对象的分页信息对象
        Page<Category> pageInfo = new Page<>(page, pageSize);
        //构造条件构造器对象
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //设置条件
        wrapper.orderByAsc(Category::getSort);
        //查询分页信息
        categoryService.page(pageInfo, wrapper);

        return R.success(pageInfo);
    }


    /**
     * 添加分类信息
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("请求参数: {} ",category.toString());
        categoryService.save(category);
        //进行mysql数据库增删改时，Redis应该清除相应的key
        redisTemplate.delete("category");
        return R.success("新增分类成功");
    }
}
